package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Section;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.SectionService;
import com.itwn.comic.vo.ResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("section")
public class SectionController {

    @Autowired
    SectionService sectionService;
    @GetMapping("/list")
    public ResponseResult<PageInfo<Section>> list(@RequestParam(value = "size",defaultValue = "5")Integer size,
                                                  @RequestParam(value = "now",defaultValue = "1")Integer now,
                                                  Integer id) {
        ResponseResult<PageInfo<Section>> result = new ResponseResult<>();
        try {
            PageInfo<Section> pageInfo = sectionService.querySectionByPage(size, now,id);
            if (pageInfo != null) {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            } else {
                result.setCode(201);
                result.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Section section){
        ResponseResult<Void> result = new ResponseResult<>();
        section.setUpdateTime(new Date());
        try{
            int i = sectionService.addSection(section);
            if(i != 0){
                result.setCode(200);
                result.setMsg("添加成功");
            }else {
                result.setCode(201);
                result.setMsg("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return  result;
    }
    @PutMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Section section){
        section.setUpdateTime(new Date());
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            int i = sectionService.modifySection(section);
            if(i != 0){
                result.setCode(200);
                result.setMsg("修改成功");
            }else {
                result.setCode(201);
                result.setMsg("修改失败");
            }
        }catch (Exception e){
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> del(@PathVariable("id")int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
           int i = sectionService.removeSectionById(id);
            if(i != 0){
                result.setCode(200);
                result.setMsg("删除成功");
            }else {
                result.setCode(201);
                result.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return  result;
    }
}
